home *** CD-ROM | disk | FTP | other *** search
/ Shareware Grab Bag / Shareware Grab Bag.iso / 007 / moredasm.arc / MOREDASM.NOT
Text File  |  1987-01-26  |  20KB  |  513 lines

  1. .lh 6
  2. .cw 7
  3. .mt 4
  4. .mb 11
  5. .pl 88
  6. .op
  7.  
  8. APPLICATIONS: Using ASMGEN to disassemble MORE.COM
  9. .he ASMGEN disassembly of MORE.COM                     Page #
  10.  
  11.      The primary justification for writing programs in assembly
  12. language is execution speed.  Few would argue in favor of such
  13. direct machine contact if there were easier ways to achieve the
  14. performance improvements that are possible in comparison with
  15. high-level programs like BASIC and Pascal.
  16.  
  17.      The best way to achieve some level of literacy in assembly
  18. language programming is to examine the programs written by
  19. others.  You can incorporate segments of their code in your
  20. programs--or modify their programs to serve your own application.
  21.  
  22.      Programs available in the public domain are ideal for this
  23. purpose, since the sheer variety of programs raises the
  24. likelihood that you will be able to find something close to what
  25. you need.  Unfortunately, they do not all come with the source
  26. code.
  27.  
  28.      For modifying programs for which you do not have the source
  29. code, there is a disassembler available (again, in the public
  30. domain) which you can use to reconstruct the basic program
  31. structure.   Named ASMGEN, it was written by Jack Gersbach and
  32. J. Damke.  (It turns out he is also the co-author of one of the
  33. utilities packages now being marketed by IBM in its Personally
  34. Developed Software series.)  It available on Windmill, Thousand
  35. Oaks, Simi, RIMS and most other public domain bulletin boards.
  36. Look for version 2.01 or later.
  37.  
  38.      We've chosen for an example the DOS filter MORE.COM.  This
  39. modest little program at 384 bytes is the smallest of the three filters
  40. supplied with DOS.  The other two, FIND.COM and SORT.COM, are
  41. considerably more complex.  MORE.COM provides in its small space
  42. assembly language examples of the following operations (in rough
  43. order of appearance in the program):
  44.  
  45.      . Checking for DOS version number (2.00 or higher)
  46.      . Activating the required input and standard error devices
  47.      . Reading standard input into a buffer
  48.      . Using DOS file handles instead of File Control Blocks
  49.      . Using a System BIOS call for checking the width of the
  50.           current screen
  51.      . Managing a "shadow screen" to keep track of the cursor
  52.      . Checking input for end of file, enter, line feed,
  53.           backspace, tab and bell (beep) characters
  54.      . Correctly adjusting screen display for 8-space tab
  55.      . Wrapping the screen display if the line width exceeds
  56.           the screen width, whether it is 40 or 80 columns wide
  57.      . Pausing the display at each full screen, and waiting for a
  58.           keypress to proceed
  59.  
  60.  
  61.      To do this sort of disassembly operation properly, you need
  62. a number of tools.  A complete list comprises the following items:
  63.  
  64.      . Disassembler: ASMGEN, V2.01 or later by Gersbach/Damke
  65.           (Public Domain)
  66.      . File compare: CMP10, V1.10 or later by Gersbach
  67.           (Public Domain)
  68.      . Concurrent text editor and utility: The Notepad and the
  69.           Calculator features of SideKick, V1.10B
  70.           from Borland International
  71.      . Main text editor: WordStar 3.3 in non-document mode
  72.           from MicroPro International
  73.      . Macro Assembler V1.00, from IBM/Microsoft
  74. .cp 5
  75.      . Manuals:
  76.           Disk Operating System Reference Manual
  77.           Disk Operating System Technical Reference Manual
  78.           Technical Reference Personal Computer XT and
  79.                Portable Personal Computer
  80.           A reference manual on the Intel 8086/8088 instruction set
  81.  
  82.      There is no point in attempting a disassembly of any size
  83. without the DOS Technical Reference Manual. IBM and Microsoft
  84. have moved all the required DOS function call information into a
  85. separate manual starting with version 2.10.  It is possible to
  86. get by with the integrated DOS version 2.00  manual for the
  87. function calls, but only as a last resort.
  88.  
  89.      The hardware manual is another absolute requirement because
  90. it is only in the System BIOS program listing that you will find
  91. what the BIOS function calls mean.  Even a program as nominally
  92. trivial as MORE.COM uses a BIOS  call to determine the width of
  93. the current display screen.  You will not be able to decode the
  94. system interrupts without this manual.
  95.  
  96.      The Macro Assembler is also required.  Unlike CP/M, DOS
  97. comes with no assembler.  The only facility it has to assemble
  98. programs is the "A" command in DEBUG, which will assemble into
  99. memory the instructions you enter via the keyboard.  This is on
  100. the same approximate level as the Apple II monitor ROM assembler.
  101.  
  102.      Since you must have the macro assembler, you might as well
  103. use its macro facility.  As you will see in the reconstructed
  104. program listing that follows, all the BIOS and DOS function calls
  105. have been recoded as macros.  This allows you to remove yourself
  106. from the requirement of remembering the purpose of each of the
  107. hexadecimal functions, and shows more clearly the underlying flow
  108. of the program in a language that you can define yourself that
  109. makes most sense to you.  This is how you can build up your DOS
  110. function library.
  111.  
  112.      Unfortunately, ASMGEN does not take its interpretation this
  113. far; it is up to you to replace the INT 21 forms with the
  114. appropriate macro call.  Equally unfortunately, the DOS Technical
  115. Reference Manual is mainly organized along the lines of the
  116. hexadecimal function number, so you may find yourself referring
  117. back to the macro definition to find the hex value of the call so
  118. that you can locate its reference in the DOS manual.  Sigh...
  119.  
  120.      It would be difficult to imagine undertaking this sort of
  121. exercise without SideKick.  SideKick has a "notepad", which can
  122. function as a concurrent text editor.  SideKick has all the
  123. features and command syntax of WordStar with the exception of
  124. word wrap (not needed in the context of programming). It remains
  125. resident so that by invoking it with the <shift><shift> keys
  126. (holding down both <shift> keys simultaneously) you can edit the
  127. .SEQ file required by ASMGEN to interpret the .COM file without
  128. the delay of reloading and then unloading the main text editor.
  129. A superior utility.
  130.  
  131.      We use WordStar for word processing and text editing.  There
  132. are other word processors available.  For this type of work,
  133. where you are doing programming and debugging, you need windows.
  134. We use SideKick for that purpose.  You might also try XyWrite II
  135. Plus (two windows), or FinalWord (multiple windows).  Pmate also
  136. has multiple edit buffers, but is thoroughly inscrutable.
  137. Microsoft Word supports up to eight windows, but the entry/exit
  138. overhead is too much and the internal format must be translated
  139. to ASCII before it can be used.  XyWrite takes a some setup time
  140. as well, but it provides a facility whereby you can drop down to
  141. DOS and back again very quickly.
  142.  
  143.      Finally, the file compare program CMP10, also by Jack
  144. Gersbach.  This too is a public domain program. The absolute
  145. first objective of a disassembly is the reconstruction of the
  146. original file--byte for byte.  The program listing that follows,
  147. with all its macro definitions, will assemble, link, EXE2BIN and
  148. REN into a .COM file that is absolutely identical to the source
  149. .COM file.  It is only after you have achieved this objective
  150. that you should feel comfortable about modifying the
  151. reconstructed .ASM file for your own purposes.
  152.  
  153.      DOS provides a file compare program (COMP.COM), but
  154. unfortunately if the files are of different lengths the utility
  155. stops.  This is of little use, since in most cases almost all
  156. efforts at reassembly except for the very last will result in
  157. .COM files of different lengths.  CMP10 will compare files of
  158. different lengths and show the differences as well--in hex and
  159. ASCII format similar to a DEBUG display listing.  Indispensable.
  160.  
  161.      The reassembled and reconstituted source file for MORE.COM
  162. follows.  The source code shown here will generate a .COM file
  163. that is absolutely identical, byte for byte, with the original
  164. DOS 2.00 version from which it was constructed.  It has been
  165. checked against DOS Version 3.00.  With the exception of a
  166. slightly shorter buffer tail at the end of the program, DOS 3.00
  167. is identical byte-for-byte to the DOS 2.00 program.
  168.  
  169.  
  170. R. Zider
  171. San Francisco, CA
  172. 11.18.84
  173. Copyright 1984.  All right reserved.
  174. Released to the public domain for personal use only.
  175. .pa
  176.                      REASSEMBLY OF MORE.COM            Exhibit 1
  177.  
  178.                        Listing of MOR.ASM
  179.                        ------------------
  180.  
  181.  
  182.  
  183. TITLE   MOR     3-8-83  [11-17-84]
  184. ;
  185. ;    Reassembly of IBM PC DOS 2.00 MORE.COM filter
  186. ;
  187. ;    R. Zider  Zider Brothers, San Francisco
  188. ;
  189. ;
  190. .RADIX        16
  191. BEL     EQU     07H
  192. BKSP    EQU     08H
  193. TAB     EQU     09H
  194. LF      EQU     0AH
  195. CR      EQU     0DH
  196. EOF     EQU     1AH
  197. ;
  198. ;                               MACRO DEFINITIONS
  199. ;
  200. ;SYSTEM BIOS MACROS
  201. ;
  202. VIDEO_BIOSCALL  MACRO
  203. ;;Invoke System BIOS video I/O function call
  204. ;;On entry:
  205. ;;(AH) = Function number
  206. ;;See page 5-68ff Technical Reference Personal Computer XT and
  207. ;;  Portable Personal Computer
  208.         INT     10H
  209.         ENDM
  210. ;
  211. GET_CURRENT_VIDEO_STATE MACRO
  212. ;;Get screen width using a video BIOS call
  213. ;;On entry:
  214. ;;If (AH) = 15 (0FH), then function returns current video state
  215. ;;On exit:
  216. ;;"(AL) = Mode currently set (see AH=0 for explanation)"
  217. ;;"(AH) = Number of character columns on screen"
  218. ;;"(BH) = Current active display page"
  219. ;;                              Quoted from TRM page 5-69
  220.         MOV     AH,0FH
  221.         VIDEO_BIOSCALL
  222.         ENDM
  223. ;
  224. ;
  225. ;DOS MACROS
  226. ;
  227. DOS_EXIT        MACRO
  228.         INT     20H
  229.         ENDM
  230. ;
  231. DOSCALL MACRO
  232. ;;Standard DOS function call
  233. ;;On entry:
  234. ;;(AH) = Function call number
  235.         INT     21H
  236.         ENDM
  237.  
  238. CLEAR_KB_INPUT  MACRO
  239. ;;Clear keyboard buffer, read char from std input device (keyboard)
  240. ;;Character returned in (AL)
  241.         MOV     AH,0CH          ;;Clear KB buffer (plus function in AL)
  242.         MOV     AL,1H           ;;Read char from KB function
  243.         DOSCALL
  244.         ENDM
  245. ;
  246. .cp 5
  247. DOS_DISPLAY_OUT MACRO
  248. ;;(DL) has the character to be displayed
  249.         MOV     AH,2H
  250.         DOSCALL
  251.         ENDM
  252. ;
  253. DOS_PRINT_STRING        MACRO   STRING_LABEL
  254. ;Output a "$"-terminated string
  255.         MOV     DX,OFFSET STRING_LABEL
  256.         MOV     AH,9H
  257.         DOSCALL
  258.         ENDM
  259. ;
  260. GET_DOS_VERSION MACRO
  261. ;;Version number returned in two pieces:
  262. ;;(AL) = Major version number  (as "02" or "03")
  263. ;;(AH) = Minor version number (as "00" or "01")
  264.         MOV     AH,30H
  265.         DOSCALL
  266.         ENDM
  267. ;
  268. CLOSE_FILE_HANDLE       MACRO
  269. ;;On entry:
  270. ;;(BX) = File handle returned by "open" or "create"
  271. ;;On exit:
  272. ;;(AX) = Error codes (NONE if carry not set)
  273.         MOV     AH,3EH
  274.         DOSCALL
  275.         ENDM
  276. ;
  277. READ_FROM_FILE  MACRO   HANDLE, BYTES, BUFFER
  278. ;;Transfer bytes from a file into a buffer
  279. ;;On entry:
  280. ;;(BX) = File handle of source file
  281. ;;(CX) = Number of bytes to be read (max--may be less than this)
  282. ;;(DX) = Location of destination buffer
  283. ;;On exit:
  284. ;;(AX) = Number of bytes actually read; or else error code if carry set
  285.         MOV     DX, OFFSET BUFFER
  286.         MOV     CX, BYTES
  287.         MOV     BX, HANDLE
  288.         MOV     AH,3FH
  289.         DOSCALL
  290.         ENDM
  291. ;
  292. DUP_FILE_HANDLE MACRO   ;;HANDLE argument not yet implemented
  293. ;;This function duplicates a file handle
  294. ;;On entry:
  295. ;;(BX) = File handle to be duplicated
  296. ;;On exit:
  297. ;;(AX) = New file handle, referring to same file at same position
  298. ;;       (or else error codes if carry set)
  299. ;;      MOV     BX, HANDLE      ;;Need conditional assembly if arg missing
  300.         MOV     AH,45H
  301.         DOSCALL
  302.         ENDM
  303. ;
  304. ;                               PROGRAM STARTS HERE
  305. ;
  306. ;
  307. ;INITIAL VALUES :       CS:IP   0000:0100
  308. ;                       SS:SP   0000:FFFF
  309. S0000   SEGMENT
  310.         ASSUME DS:S0000, SS:S0000 ,CS:S0000 ,ES:S0000
  311.         ORG     $+0100H
  312. START:  GET_DOS_VERSION                 ;Check DOS version first
  313.         XCHG    AH,AL                   ;Put major version in AH, minor in AL
  314.         CMP     AX,200                  ;Must be 2.00 or higher
  315.         JNB     OK_VER_200
  316.         DOS_PRINT_STRING  WRONG_DOS     ;Nope..
  317.         DOS_EXIT                        ;..bye
  318. .cp 5
  319. OK_VER_200:
  320.         MOV     BYTE PTR SCREEN_HEIGHT,19       ;Screen height 25 lines
  321.         GET_CURRENT_VIDEO_STATE         ;Not sure of width, though--get it
  322.         MOV     SCREEN_WIDTH,AH         ;OK, save current width
  323.         DOS_PRINT_STRING        CR_LF   ;Flush screen with a CR-LF
  324.         XOR     BX,BX                   ;Zero BX to get a duplicate..
  325.         DUP_FILE_HANDLE                 ;..file handle of std input (handle 0)
  326.         MOV     BP,AX                   ;New file handle to BP
  327.         CLOSE_FILE_HANDLE               ;Close handle 0 (std input)--use dup
  328.         MOV     BX,2                    ;Default handle 2 is std error output..
  329.         DUP_FILE_HANDLE                 ;..device--activate it
  330.  
  331. READ_STD_INPUT_INTO_BUFFER:
  332.         CLD                             ;Clear direction
  333. ;       MOV     DX,OFFSET BUFFER        ;Destination buffer
  334. ;       MOV     CX,1000                 ;Max of 1000H bytes (4KB)
  335. ;       MOV     BX,BP                   ;This has new std input file handle
  336.         READ_FROM_FILE BP 1000 BUFFER   ;OK, read on..
  337.                                         ;..and ignore any error return
  338.         OR      AX,AX                   ;Check for non-zero bytes read
  339.         JNZ     OK_READ                 ;"OK" if non-zero
  340. EXIT:   DOS_EXIT                        ;Empty std input file--bye
  341.  
  342. OK_READ:
  343.         MOV     CX,AX                   ;Number of bytes read to CX
  344.         MOV     SI,DX                   ;Buffer location to SI (now source)
  345. LOAD_NEXT_BYTE:
  346.         LODSB                           ;Start processing bytes..
  347.         CMP     AL,EOF                  ;..looking for an end-of-file (1AH)..
  348.         JZ      EXIT                    ;..and exit if found
  349.         CMP     AL,CR                   ;Check if it is a carriage return..
  350.         JNZ     CK_LINEFEED             ;..and if not, continue on with check
  351.         MOV     BYTE PTR COLUMN_COUNT,1 ;CR, so reset to screen column 1 and..
  352.         JMP     SHORT   DISPLAY_OUT     ;..output the CR to the screen
  353.  
  354. CK_LINEFEED:
  355.         CMP     AL,LF                   ;Check if it is a line feed (OAH)..
  356.         JNZ     CK_BKSPACE              ;..and if not, go to next char check
  357.         INC     BYTE PTR LINE_COUNT     ;LF, so bump the line count and..
  358.         JMP     SHORT   DISPLAY_OUT     ;..output the LF to the screen
  359.  
  360. CK_BKSPACE:
  361.         CMP     AL,BKSP                 ;Check for backspace char (08H)..
  362.         JNZ     CK_TAB                  ;..and if not, continue on to next
  363.         CMP     BYTE PTR COLUMN_COUNT,1 ;Is backspace, check if at column 1..
  364.         JZ      DISPLAY_OUT             ;..and if so, just output the char
  365.         DEC     BYTE PTR COLUMN_COUNT   ;Not at column 1, so dec the column..
  366.         JMP     SHORT   DISPLAY_OUT     ;..and output the backspace
  367.  
  368. CK_TAB: CMP     AL,TAB                  ;Check for tab char (09H)..
  369.         JNZ     CK_BEL                  ;..and if not, press on
  370.         MOV     AH,COLUMN_COUNT         ;Is tab, so get the current column..
  371.         ADD     AH,7                    ;..add 7 to it..
  372.         AND     AH,0F8                  ;..and mask for MOD 8..
  373.         INC     AH                      ;..adding one..
  374.         MOV     COLUMN_COUNT,AH         ;..and make this the new column..
  375.         JMP     SHORT   DISPLAY_OUT     ;..and output it to the display
  376.  
  377. CK_BEL: CMP     AL,BEL                  ;Check for BEL (beep) char (07)
  378.         JZ      DISPLAY_OUT             ;Is a BEL, so let console handle it
  379.         INC     BYTE PTR COLUMN_COUNT   ;None of these, so bump the column..
  380.         MOV     AH,COLUMN_COUNT         ;..and check to see if we're at the
  381.         CMP     AH,SCREEN_WIDTH         ;..edge of the screen..
  382.         JBE     DISPLAY_OUT             ;..and if not, output the character
  383.         INC     BYTE PTR LINE_COUNT     ;At screen edge, so do a line wrap..
  384.         MOV     BYTE PTR COLUMN_COUNT,1 ;..and bump the line and reset to col 1
  385.  
  386. DISPLAY_OUT:
  387.         MOV     DL,AL                   ;Output the character..
  388.         DOS_DISPLAY_OUT                 ;..to the display via DOS call
  389.         MOV     AH,LINE_COUNT           ;Check the line count..
  390.         CMP     AH,SCREEN_HEIGHT        ;..against the screen height..
  391.         JB      NEXT_BYTE               ;..and continue if no overflow
  392.         DOS_PRINT_STRING        MORE_LINE       ;"--More--" message..
  393.         CLEAR_KB_INPUT                  ;..and pause for keyboard input
  394.         DOS_PRINT_STRING        CR_LF   ;OK, continue and clear More line..
  395.         MOV     BYTE PTR COLUMN_COUNT,1 ;..reset screen pointers to Home..
  396.         MOV     BYTE PTR LINE_COUNT,1   ;..at top left position (1,1)
  397.         DEC     SI                      ;Back up one byte in buffer so that..
  398.         INC     CX                      ;..the byte can be re-sent
  399.  
  400. NEXT_BYTE:
  401.         DEC     CX                      ;One less byte to do, and..
  402.         JZ      RD_NEXT_4K              ;..if now zero, read another 4KB
  403.         JMP     LOAD_NEXT_BYTE          ;A computer's work is never done
  404.  
  405. RD_NEXT_4K:
  406.         JMP     READ_STD_INPUT_INTO_BUFFER   ;Heigh Ho, Heigh Ho...
  407.  
  408. SCREEN_HEIGHT   EQU     $
  409.         DB      18                      ;24 lines, but gets overwritten
  410. SCREEN_WIDTH    EQU     $
  411.         DB      50                      ;Here, 80 columns wide (overwtritten)
  412. LINE_COUNT      EQU     $
  413.         DB      1                       ;Starts at 1
  414. COLUMN_COUNT    EQU     $
  415.         DB      1,5 DUP(0)              ;Starts at 1
  416. MORE_LINE       EQU     $
  417.         DB      CR,'-- More --$'        ;Displayed on 25th line
  418. WRONG_DOS       EQU     $
  419.         DB      'MORE: Incorrect DOS version'
  420. CR_LF           EQU     $
  421.         DB      CR,LF,'$'
  422. BUFFER          EQU     $
  423.         DB      66 DUP(0)               ;Read buffer only 66H bytes here
  424.         S0000   ENDS
  425. ;
  426. END     START
  427.  
  428. .pa
  429.                             MORE.ASM                   Exhibit 2
  430.  
  431.                  Assembly and link to .COM file
  432.                  ------------------------------
  433.  
  434.      This is what you will have to do to get a .COM file that you
  435. can compare with the original MORE.COM program in DOS 2.00.  The
  436. following listing has been captured from the screen display with
  437. FSPOOL.COM.
  438.  
  439. B>dir
  440.  
  441.  Volume in drive B is PCDISASM
  442.  Directory of  B:\mor
  443.  
  444. .            <DIR>     11-04-84   2:17p
  445. ..           <DIR>     11-04-84   2:17p
  446. M        OBJ      536  11-17-84   7:24p
  447. MOR      ASM     2468  11-17-84   9:10a
  448. M        LST    15785  11-17-84   7:24p
  449. M        COM      384  11-17-84   7:21p
  450. MOR      OBJ      501  11-17-84  11:53a
  451. MOR      SEQ      617  11-17-84   9:07a
  452. M        EXE     1152  11-17-84   7:24p
  453. MORE300  COM      320   8-14-84   8:00a
  454. MORE     COM      384   3-08-83  12:00p
  455. M        BIN      384  11-17-84   7:24p
  456. M        ASM     7168  11-17-84   4:39p
  457. MOR      COM      384  11-17-84  11:54a
  458. N        LST    15785  11-17-84   7:14p
  459. NOTES            3525  11-17-84   7:04p
  460. T        PRN      512  11-17-84   7:44p
  461.        17 File(s)    181248 bytes free
  462.  
  463. B>masm m
  464. Bad command or file name
  465.  
  466. B>a:ini
  467.  
  468. B>path=a:\
  469. B>masm m
  470. The IBM Personal Computer MACRO Assembler
  471. Version 1.00 (C)Copyright IBM Corp 1981
  472.  
  473. Object filename [M.OBJ]:
  474. Source listing  [NUL.LST]: m.lst
  475. Cross reference [NUL.CRF]:
  476.  
  477. Warning Severe
  478. Errors  Errors
  479. 0       0
  480.  
  481. B>link m
  482.  
  483. IBM Personal Computer Linker
  484. Version 2.00 (C)Copyright IBM Corp 1981, 1982, 1983
  485.  
  486. Run File [M.EXE]:
  487. List File [NUL.MAP]:
  488. Libraries [.LIB]:
  489. Warning: No STACK segment
  490.  
  491. There was 1 error detected.
  492.  
  493. B>exe2bin m
  494.  
  495. B>cmp10 m.bin more.com
  496. Files are identical
  497.  
  498. .cp 15
  499. B>dir m.*
  500.  
  501.  Volume in drive B is PCDISASM
  502.  Directory of  B:\mor
  503.  
  504. M        OBJ      536  11-17-84   7:46p
  505. M        LST    15785  11-17-84   7:46p
  506. M        COM      384  11-17-84   7:21p
  507. M        EXE     1152  11-17-84   7:47p
  508. M        BIN      384  11-17-84   7:47p
  509. M        ASM     7168  11-17-84   4:39p
  510.         6 File(s)    180224 bytes free
  511.  
  512. B>fspool
  513.